package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.service.UacGroupFeignApi;
import com.csun.cmny.provider.model.service.UacRoleFeignApi;
import com.csun.cmny.provider.model.service.UacUserFeignApi;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-09 17:03
 * @Description:
 */
@Slf4j
@Service
public class UacRpcServiceImpl implements UacRpcService {

    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private UacRoleFeignApi uacRoleFeignApi;
    @Resource
    private UacGroupFeignApi uacGroupFeignApi;

    /**
     * query opc user vo by user id
     *
     * @param userId the user id
     * @return the opc user vo
     */
    @Override
    public OpcUserVo queryOpcUserVoByUserId(Long userId) {

        try {
            Wrapper<OpcUserVo> wrapper = uacUserFeignApi.queryOpcUserVoByUserId(userId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10018002);
            }

            OpcUserVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryOpcUserVoByUserId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query language by group id
     *
     * @param groupId the group id
     * @return the group language vo
     */
    @Override
    public GroupLanguageVo queryLanguageByGroupId(Long groupId) {
        return null;
    }

    /**
     * 根据用户Id查询角色列表
     *
     * @param userId user id
     *
     * @return
     */
    @Override
    public List<RpcRoleVo> queryRoleListByUserId(Long userId) {

        try {
            Wrapper<List<RpcRoleVo>> wrapper = uacRoleFeignApi.queryRoleListByUserId(userId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<RpcRoleVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryRoleListByUserId={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 查询机构列表
     *
     * @return
     */
    @Override
    public List<RpcGroupVo> queryGroupList() {

        try {
            Wrapper<List<RpcGroupVo>> wrapper = uacGroupFeignApi.queryGroupList();
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<RpcGroupVo> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("queryGroupList={}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 查询该机构及子机构信息
     *
     * @param groupId   the group id
     *
     * @return  the rpc group vo list.
     */
    @Override
    public List<GroupVo> queryGroupListByGroupId(Long groupId) {

        try {
            Wrapper<List<GroupVo>> wrapper = uacGroupFeignApi.queryGroupListByGroupId(groupId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10013012);
            }

            List<GroupVo> result = wrapper.getResult();

            assert result != null;

            return result;

        } catch (Exception e) {
            log.error("queryGroupListByGroupId={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<GroupInforVo> getAllGroup(Long groupId) {
        return uacGroupFeignApi.getAllGroup(groupId);
    }
}
